博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
邮件发送
阅读量:5278 次
发布时间:2019-06-14

本文共 6981 字,大约阅读时间需要 23 分钟。

1:整合springboot的发送参见:

https://blog.csdn.net/zyw_java/article/details/81635375

2:常见的邮箱服务器地址:

https://blog.csdn.net/chuanyu/article/details/46740287

3:如何查询公司的outlook邮箱服务器地址:

https://zhidao.baidu.com/question/39774692.html

 

关键点:

密码为三方授权码

待解决问题:

如何查询企业邮箱的服务地址?

 

demo

org.springframework.boot
spring-boot-starter-mail

配置

spring:  application:    name: test-config-server  profiles:  mail:    host: smtp.163.com #发送邮件服务器    username: ppyun7c@163.com #163邮箱    password: wdsqm01 #客户端授权码    protocol: smtp #发送邮件协议    properties.mail.smtp.auth: true    properties.mail.smtp.port: 465 #端口号465或994    properties.mail.display.sendmail: XIEDONGXU #可以任意    properties.mail.display.sendname: Spring Boot Guide Email #可以任意    properties.mail.smtp.starttls.enable: true    properties.mail.smtp.starttls.required: true    properties.mail.smtp.ssl.enable: true    default-encoding: utf-8    from: ppyun7c@163.com #与上面的username保持一致

 

代码

package com.test.domi.service;import javax.mail.MessagingException;public interface IMailService {    /**     * 发送文本邮件     * @param to     * @param subject     * @param content     */    public void sendSimpleMail(String to, String subject, String content, String... cc);    /**     * 发送HTML邮件     * @param to     * @param subject     * @param content     * @throws MessagingException     */    public void sendHtmlMail(String to, String subject, String content, String... cc)throws MessagingException;    /**     * 发送带附件的邮件     * @param to     * @param subject     * @param content     * @param filePath     * @throws MessagingException     */    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException;    /**     * 发送正文中有静态资源的邮件     * @param to     * @param subject     * @param content     * @param rscPath     * @param rscId     * @throws MessagingException     */    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc)throws MessagingException;}

 实现类

package com.test.domi.service.impl;import com.test.domi.service.IMailService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.io.FileSystemResource;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import org.springframework.stereotype.Component;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;@Componentpublic class IMailServiceImpl implements IMailService {    @Autowired    private JavaMailSender mailSender;    @Value("${spring.mail.from}")    private String from;    /**     * 发送文本邮件     * @param to     * @param subject     * @param content     */    @Override    public void sendSimpleMail(String to, String subject, String content, String... cc) {        SimpleMailMessage message = new SimpleMailMessage();        message.setFrom(from);        message.setTo(to);        message.setCc(cc);        message.setSubject(subject);        message.setText(content);        mailSender.send(message);    }    /**     * 发送HTML邮件     * @param to     * @param subject     * @param content     */    @Override    public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException{        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        mailSender.send(message);    }    /**     * 发送带附件的邮件     * @param to     * @param subject     * @param content     * @param filePath     * @throws MessagingException     */    @Override    public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc)throws MessagingException {        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        FileSystemResource file = new FileSystemResource(new File(filePath));        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));        helper.addAttachment(fileName, file);        mailSender.send(message);    }    /**     * 发送正文中有静态资源的邮件     * @param to     * @param subject     * @param content     * @param rscPath     * @param rscId     */    @Override    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {        MimeMessage message = mailSender.createMimeMessage();        MimeMessageHelper helper = new MimeMessageHelper(message, true);        helper.setFrom(from);        helper.setTo(to);        helper.setCc(cc);        helper.setSubject(subject);        helper.setText(content, true);        FileSystemResource res = new FileSystemResource(new File(rscPath));        helper.addInline(rscId, res);        mailSender.send(message);    }}

 

 

controller

package com.test.domi.controller;import com.test.domi.common.utils.ResultInfo;import com.test.domi.common.utils.ResultUtil;import com.test.domi.service.impl.IMailServiceImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/email")public class EmailController {    @Autowired    private IMailServiceImpl mailService;//注入发送邮件的各种实现方法    @GetMapping("/normal")    public ResultInfo index(){        try {            mailService.sendSimpleMail("491431567@qq.com","SpringBoot Email","这是一封普通的SpringBoot测试邮件");            String[] cc = {"491431567@qq.com","491431567@qq.com"};            mailService.sendAttachmentsMail("491431567@qq.com","附件发送测试","这是附件","C:\\Users\\Domi\\Desktop\\ls.txt",cc);            mailService.sendHtmlMail("491431567@qq.com","html测试","\n"                    + "    
\n" +"

欢迎使用IJPay -By Javen

\n" +"
https://github.com/Javen205/IJPay" + "
IJPay 让支付触手可及,欢迎Start支持项目发展:)
\n" + "
如果对你有帮助,请任意打赏\n" + "
\n" + " \n" + "",cc); }catch (Exception ex){ ex.printStackTrace(); return ResultUtil.getFailResult("999999","失败",null); } return ResultUtil.getSuccessResult(null); }}

 

转载于:https://www.cnblogs.com/domi22/p/9749024.html

你可能感兴趣的文章
27. Retrofit2 -- How to Use Dynamic Urls for Requests
查看>>
一个简易的选择小时(时分秒)的插件
查看>>
Burpsuite之Burp Collaborator模块介绍
查看>>
通过宏定义将__declspec(dllexport)与__declspec(dllimport)的转化,实现库代码和使用代码使用同一份头文件...
查看>>
CentOS7.5安装坚果云
查看>>
Http协议
查看>>
假如一个js语句
查看>>
ARP协议格式、ARP运行机制入门学习
查看>>
10.矩形覆盖
查看>>
Bootstrap 实例 - 模态框(Modal)插件
查看>>
SaltStack
查看>>
[Preact] Integrate react-router with Preact
查看>>
[TypeScript] Configuring TypeScript Which Files to Compile with "Files" and "OutDir"
查看>>
2018-2019赛季多校联合新生训练赛第八场(2018/12/22)补题题解
查看>>
洛谷P2802 回家
查看>>
SpringMVC工作原理2(代码详解)
查看>>
退出循环break,在while、for、do...while、循环中使用break语句退出当前循环,直接执行后面的代码。...
查看>>
正则表达式入门及实例
查看>>
linux运维升级路线
查看>>
python3插入数据
查看>>